﻿var strCode = "";
var url = "http://localhost:56240/";
function genCode(reload) {
    var w = $("#txtWidth").val();
    var h = $("#txtHeight").val();

    var border = ($("#chkBorder").attr("checked") == true ? "1" : "0");
    strCode = "<script type='text/javascript'>";
    strCode += "vdco_width='" + w + "';";
    strCode += "vdco_height='" + h + "';";
    strCode += "vdco_border = " + border + ";";
    strCode += "vdco_text_color='" + $('#select_text_color div').css('backgroundColor') + "';";
    strCode += "vdco_text_hover_color='" + $('#select_text_hover_color div').css('backgroundColor') + "';";
    strCode += "vdco_bg_color='" + $('#select_bg_color div').css('backgroundColor') + "';";
    strCode += "vdco_bg_hover_color='" + $('#select_bg_hover_color div').css('backgroundColor') + "';";
    strCode += "</script>";
    strCode += "<script type='text/javascript' src='"+url+"js/widget.js'></script>";
    strCode += "<script type='text/javascript' src='" + url + "Widget.aspx?service="+encodeURIComponent(getserviceURL())+"'></script>";
    $("#txtCode").val(strCode);
    if (reload)
        $("#wg_main").load("Widget.aspx?writescript=false");
}
function getserviceURL()
{
    var linkReturn=$(".megawg_right_menu_top a").attr("href");
    var param="service=";
    var link=linkReturn.substring(0,linkReturn.indexOf(param)+param.length);
    link=link+$("#txtServiceURL").val();
    return link;
}
$(function() {
    genCode();
    $(".megawg_right_menu_top a").attr("href",getserviceURL());
    //--------------- Color Picker ------------------------
    $('#select_text_color').ColorPicker({
        color: '#FFF',
        onShow: function(colpkr) {
            $(colpkr).fadeIn(500);
            return false;
        },
        onHide: function(colpkr) {
            $(colpkr).fadeOut(500);
            return false;
        },
        onChange: function(hsb, hex, rgb) {
            $('#select_text_color div').css('backgroundColor', '#' + hex);
            $('ul.megawg_top_menu li a').css('color','#'+hex);
            genCode(false);

        }
    });
    $('#select_text_hover_color').ColorPicker({
        color: '#FFF',
        onShow: function(colpkr) {
            $(colpkr).fadeIn(500);
            return false;
        },
        onHide: function(colpkr) {
            $(colpkr).fadeOut(500);
            return false;
        },
        onChange: function(hsb, hex, rgb) {
            $('#select_text_hover_color div').css('backgroundColor', '#' + hex);
            $('ul.megawg_top_menu li a').hover(function()
            {
                $(this).css('color','#'+hex);
            },function()
            {
                $(this).css('color',$('#select_text_color div').css('backgroundColor'));
            });
            genCode(false);

        }
    });
    $('#select_bg_color').ColorPicker({
        color: '#E67817',
        onShow: function(colpkr) {
            $(colpkr).fadeIn(500);
            return false;
        },
        onHide: function(colpkr) {
            $(colpkr).fadeOut(500);
            return false;
        },
        onChange: function(hsb, hex, rgb) {
            $('#select_bg_color div,.megawg_menutop,.wg_sub_nav').css('backgroundColor', '#' + hex);            
            genCode(false);

        }
    });
    $('#select_bg_hover_color').ColorPicker({
        color: '#E67817',
        onShow: function(colpkr) {
            $(colpkr).fadeIn(500);
            return false;
        },
        onHide: function(colpkr) {
            $(colpkr).fadeOut(500);
            return false;
        },
        onChange: function(hsb, hex, rgb) {
            $('#select_bg_hover_color div').css('backgroundColor', '#' + hex);
            $('.megawg_top_menu li,.wg_sub_nav li').hover(function()
            {
                $(this).css('backgroundColor', '#' + hex);
            },function()
            {
                $(this).css('backgroundColor', null);  
            });
            genCode(false);

        }
    });
    $("#txtCode").click(
        function() {
            $(this).select();
        }
    );
    //--------------- Color Picker ------------------------
    
    $("#txtWidth,#txtHeight").blur(
        function() {
            genCode(false);
            $(".megawg_menutop").css("width", $("#txtWidth").val());
            $(".megawg_menutop").css("height", $("#txtHeight").val());
        }
    );
    $("#txtServiceURL").blur(
        function()
        {
            genCode(false);
            $(".megawg_right_menu_top a").attr("href",getserviceURL());
        }
    );
    $("#chkBorder").click(function() {
        genCode(false);
        var borderStyle = "solid " + ($(this).attr("checked") == true ? "1" : "0") + "px gray";
        $(".megawg_menutop").css("border", borderStyle);
    }
    );
}
);